package cn.iocoder.yudao.module.handguard.tool;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;

@Slf4j
public class Base64ToFile {
//    public static void base64ToFile(String base64String, String filePath) throws IOException {
//        byte[] decodedBytes = Base64.getDecoder().decode(base64String);
//        File file = new File(filePath);
//        FileOutputStream fileOutputStream = new FileOutputStream(file);
//        fileOutputStream.write(decodedBytes);
//        fileOutputStream.close();
//    }

    public static File base64ToFile(String base64, String filePath) {
        // 解码Base64字符串
        byte[] decodedBytes = Base64.getDecoder().decode(base64);
        File file = new File(filePath);
        try {
            // 将字节写入文件
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(decodedBytes);
            fileOutputStream.close();
            System.out.println("File created at: " + file.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

//    public static void main(String[] args) {
//        // 示例Base64编码字符串
//        String base64String = "iVBORw0KGgoAAAANSUhEUgAAA..."; // 这里应该是完整的Base64编码字符串
//
//        // 要保存的文件路径
//        String filePath = "output.png";
//
//        try {
//            base64ToFile(base64String, filePath);
//            System.out.println("File saved successfully at: " + filePath);
//        } catch (IOException e) {
//            System.out.println("Error while saving file: " + e.getMessage());
//        }
//    }
}